<?php

//判断是否GET提交
if ( ! function_exists('is_get'))
{
	function is_get(){
		return $_SERVER['REQUEST_METHOD'] == 'GET' ? TRUE : FALSE;
	}
}

//判断是否POST提交
if ( ! function_exists('is_post'))
{
	function is_post(){
		return $_SERVER['REQUEST_METHOD'] == 'POST' ? TRUE : FALSE;
	}
}

/*
 * 获取$_GET值
 * index => 变量名
 * $xss_clean => 是否清除xss，默认不清除
 */
if ( ! function_exists('get'))
{
	function get($index = NULL, $xss_clean = FALSE)
	{
		$ci = & get_instance();
		$value = $ci->input->get($index, $xss_clean);
		return is_string($value) ? trim($value) : $value;
	}
}

/*
 * 获取$_POST值
 * index => 变量名
 * $xss_clean => 是否清除xss，默认不清除
 */
if ( ! function_exists('post'))
{
	function post($index = NULL, $xss_clean = FALSE)
	{
		$ci = & get_instance();
		$value = $ci->input->post($index, $xss_clean);
		return is_string($value) ? trim($value) : $value;
	}
}

/*
 * 返回http头信息数组 apache特有 其他如ngnix没有 这里自定定义
 */
if ( ! function_exists('getallheaders'))
{
	function getallheaders()
	{
		foreach ($_SERVER as $name => $value)
		{
			if (substr($name, 0, 5) == 'HTTP_')
			{
				$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
			}
		}  
		return $headers;  
	} 
}

/*
 * 返回uri的片段,从1开始
 * n => uri片段的第几位
 * 存在返回片段值，否则返回false
 */
if ( ! function_exists('segment'))
{
	function segment($n)
	{
		$ci = & get_instance();
		return $ci->uri->segment($n);
	}
}

// 客服端ip地址
if ( ! function_exists('ip_address'))
{
	function ip_address()
	{
		$ci = & get_instance();
		return $ci->input->ip_address();
	}
}

//返回站点配置信息
if ( ! function_exists('site'))
{
	function site($index = NULL)
	{
		$ci = & get_instance();
		$ci->config->load('site', TRUE);
		return ($index === NULL) ? $ci->config->item('site') : $ci->config->item($index, 'site');
	}
}

if ( ! function_exists('widget'))
{
	function widget($name, $data = NULL)
	{
		require_once FCPATH.APPPATH.'libraries/Widget.php';

		$arr = explode('/', $name);
		$path = isset($arr[1]) ? implode('/', array_slice($arr, 0, count($arr) - 1)): '';
		$class = end($arr);

		$class_path = FCPATH.APPPATH.'widgets/'.$path.'/'.$class.'.php';
		if ( ! file_exists($class_path))
		{
			return 'widget '.$class.' file is not found';
		}

		require_once $class_path;

		$class = ucfirst($class).'_widget';

		if ( ! class_exists($class))
		{
			return 'widget '.$class.' class is not found';
		}

		$widget = new $class($data);

		return $widget->render();
	}
}

//提示（信息，跳转地址，等待时间）
if( ! function_exists('msg'))
{
	function msg($message = '' , $url = '' , $time = 2, $title = '系统提示')
	{
		//让网页过期
		header('Cache-control: private');

		if($url)
		{
			if(preg_match('/^-?[1-9]\d*$/i' , $url))
			{
				$action = '<script type="text/javascript"> function back(){history.go('.$url.');} setTimeout(back,'.($time*1000).');</script>';
				$link = '<a href="javascript:;" title="若您的浏览器没反应请点击这里！" onclick="history.go('.$url.')">若您的浏览器没反应请点击这里！</a>';
			}
			else
			{
				$action = '<meta http-equiv="refresh" content="'.$time.';url='.$url.'" />';
				$link = '<a href="'.$url.'" title="若您的浏览器没反应请点击这里！">若您的浏览器没反应请点击这里！</a>';
			}
		}
		else
		{
			$action = '';
			$link = '';
		}
		$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
		$html .= '<html xmlns="http://www.w3.org/1999/xhtml">';
		$html .= '<head>';
		$html .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		$html .= '<title>'.$title.'</title>';
		$html .= '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />';
		$html .= $action;
		$html .= '<style type="text/css">';
		$html .= '*{margin:0px;padding:0px}';
		$html .= 'body{font-family:"微软雅黑"; font-size:14px}';
		$html .= 'a{color:#57A000}';
		$html .= '.main{width:400px; margin:0px auto; margin-top:10%; border:1px solid #E2E2E2; border-radius:4px;}';
		$html .= '.title{width:385px; padding-left:15px; height:35px; line-height:35px; background-color:#EFF0F1; border-bottom:1px solid #E2E2E2; border-top-left-radius:4px; border-top-right-radius:4px;}';
		$html .= '.message{color:#333; padding:10px 15px; min-height:100px; _height:100px;}';
		$html .= '.link{padding:5px 0; text-align:center;}';
		$html .= '</style>';
		$html .= '</head>';
		$html .= '<body>';
		$html .= '<div class="main">';
		$html .= '<div class="title">';
		$html .= '系统提示';
		$html .= '</div>';
		$html .= '<div class="message">';
		$html .= $message;
		$html .= '</div>';
		$html .= '<div class="link">';
		$html .= $link;
		$html .= '</div>';
		$html .= '</div>';
		$html .= '</body>';
		$html .= '</html>';
		exit($html);
	}
}

//输出ajax数据（数据） 数据以json格式输出
if ( ! function_exists('ajax'))
{
	function ajax($result)
	{
		if (is_array($result) && $result)
		{
			$result = json_encode($result);
		}
		echo (string)$result;
		exit();
	}
}

//编辑器（名称，值，宽度，高度，是否文件管理）
if ( ! function_exists('editor'))
{
	function editor($name = 'content', $value = '', $width = 700, $height = 400, $file_manage = TRUE)
	{
		$ci = &get_instance();
		$ci->load->library('kindeditor');
		$config = array(
			'path' => base_url('assets/js/kindeditor'),
			'file_upload_url' => site_url('kindeditor/upload'),
			'file_manage_url' => site_url('kindeditor/manage')
		);
		$ci->kindeditor->config($config)->init();
		return $ci->kindeditor->show($name, $value, $width, $height, $file_manage);
	}
}